#pragma once
#include <iostream>
#include<algorithm>
#include<assert.h>
#include<cstdlib>
using namespace std;                                                         

namespace sl
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		size_t capacity()const
		{
			return _end_of_storge - _start;
		}
		size_t size()
		{
			return _finish - _start;
		}
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin()const
		{
			return _start;
		}
		const_iterator end()const
		{
			return _finish;
		}

		vector():_start(nullptr),_finish(nullptr),_end_of_storge(nullptr){}

		vector(initializer_list<T> il)
		{
			reserve(il.size());
			for (auto& e : il)
			{
				push_back(e);
			}
		}

		vector<T>& operator=(vector<T> v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storge, v._end_of_storge);

			return *this;
		}

		vector(const vector<T>& other)
		{
			reserve(other.capacity());
			for (const auto& e : other) { 
				push_back(e);
			}
		}

		vector(size_t n, const T & val = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = _finish = _end_of_storge = nullptr;
			}
		}
		
		void push_back(const T& a)
		{
			if (_finish == _end_of_storge)
			{
				size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
				reserve(newcapacity);
			}
			*_finish = a;
			_finish++;
		}

		void resize(size_t n, T val = T())
		{
			if (n > size())
			{
				reserve(n);
				while (_finish != _start + n)
				{
					*_finish = val;
					++_finish;
				}
			}
			else
			{
				_finish = _start + n;
			}
		}
	/*	void reserve(size_t n)
		{
			if (capacity() < n)
			{				
				iterator tmp = new T[n];
				size_t oldsize = size();
				std::memcpy(tmp, _start, oldsize * sizeof(T));
				delete[] _start;
				_start = tmp;
				_finish = _start + oldsize;
				_end_of_storge = _start + n;
			}
		}*/

		void reserve(size_t n)
		{
			if (capacity() < n)
			{
				T* tmp = new T[n];
				size_t oldsize = size();
				if (tmp)
				{
					for (size_t i = 0; i < oldsize; i++)
					{
						tmp[i] = _start[i];
					}
					delete[] _start;
				}

				_start = tmp;
				_finish = _start + oldsize;
				_end_of_storge = _start + n;
			}
		}

		void pop_back()
		{
			assert(_finish > _start);
				* _finish--;
		}

		void insert(iterator pos,const T& x)
		{
			assert(pos >= _start && pos <= _finish);
			if (_finish == _end_of_storge)
			{
				size_t len = pos - _start;
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
				pos = _start + len;
			}

			iterator it = _finish - 1;
			while (it >= pos)
			{
				*(it + 1) = *it;
				--it;
			}

			*pos = x;
			++_finish;
		}

		void erase(iterator pos)
		{
			assert(pos >= _start && pos <= _finish);
			iterator it = pos;
			while (it<_finish)
			{
				*it = *(it + 1);
				it++;
			}	
			_finish--;
		}

	private:
		iterator _start;
		iterator _finish;
		iterator _end_of_storge;
	};
}


